package com.ww.editsms;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.chinawangw.R;
import com.ww.editsms.db.Cursordb;
import com.ww.editsms.entity.SMS;
import com.ww.editsms.util.SmsUtil;
import com.ww.editsms.widget.wheel.WheelDate;
import com.ww.editsms.widget.wheel.WheelTime;

public class SmsActivity extends MyActivity {
	static EditText editText;
	int operateType = 1;// 1，新增来信息，2新增发信息，3修改来信息，4修改去信息
	static String phone;
	Button date;
	Button time;
	static String id;
	static int type = 1;
	static String threadId;
	String name = "sdg";
	int m_year, m_month, m_day, m_hour, m_minute;
	int result = 1;
	private boolean bool = true;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.modify);
		Bundle myBundle = this.getIntent().getExtras();
		SMS sms = myBundle.getParcelable("sms");
		operateType = sms.getOperateType();
		phone = sms.getPhoneNum();
		name = phone;
		id = sms.getId() + "";
		threadId = sms.getThreadId() + "";
		type = sms.getType();
		setUI(sms);
		titleOPeration();
	}

	public void setUI(SMS sms) {
		editText = (EditText) findViewById(R.id.smsbody);
		editText.setText(sms.getBody());
		date = (Button) findViewById(R.id.date);
		time = (Button) findViewById(R.id.time);
		getTime(sms.getTime());
		date.setText(m_year + "-" + m_month + "-" + m_day);
		time.setText(m_hour + ":" + m_minute);
		Button cancel = (Button) findViewById(R.id.cancel);
		cancel.setOnClickListener(cancelClick);
		date.setOnClickListener(dateDialog);
		time.setOnClickListener(timeDialog);
	}

	// 1，新增来信息，2新增发信息，3修改来信息，4修改去信息
	OnClickListener saveListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			if (SmsUtil.isNotEmpty(editText.getText().toString())) {
				showDialog(2);
				new Thread() {
					public void run() {
						result = 0;
						SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
						String time = m_year + "-" + m_month + "-" + m_day + " " + m_hour + ":" + m_minute;
						System.out.println("time:" + time);
						Date date = new Date();
						try {
							date = format.parse(time);
						} catch (ParseException e) {
							e.printStackTrace();
						}
						if (operateType == 1) {
							bool = Cursordb.insert(SmsActivity.this, phone, editText.getText().toString(), 1,
									date.getTime() + "");
						} else if (operateType == 2) {
							bool = Cursordb.insert(SmsActivity.this, phone, editText.getText().toString(), 2,
									date.getTime() + "");
						} else if (operateType == 3) {
							bool = Cursordb.save(SmsActivity.this, threadId, id, date.getTime() + "", editText
									.getText().toString(), type, phone);
						} else if (operateType == 4) {
							bool = Cursordb.save(SmsActivity.this, threadId, id, date.getTime() + "", editText
									.getText().toString(), type, phone);
						}

						handler.sendEmptyMessage(1);
					}
				}.start();
			} else {
				Toast.makeText(SmsActivity.this, "信息内容不能为空！", Toast.LENGTH_SHORT).show();
			}
		}
	};

	Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			if (bool) {
				Intent aintent = new Intent(SmsActivity.this, SmsListActivity.class);
				setResult(result, aintent); // 这理有2个参数(int resultCode, Intent
				Intent i = new Intent();
				i.setAction("refresh");
				sendBroadcast(i);

				if (operateType == 1 || operateType == 2) {
					showMessage("新增成功");
				} else if (operateType == 3 || operateType == 4) {
					showMessage("修改成功");
				}
				finish();
			} else {
				if (operateType == 1 || operateType == 2) {
					showMessage("新增失败");
				} else if (operateType == 3 || operateType == 4) {
					showMessage("修改失败");
				}
			}
		};
	};

	OnClickListener dateDialog = new OnClickListener() {
		@Override
		public void onClick(View v) {
			final WheelDate w = new WheelDate(SmsActivity.this);
			w.show(date.getText().toString(), new OnClickListener() {
				@Override
				public void onClick(View v) {
					String t = w.getResult();
					date.setText(t);
					String[] s = t.split("-");
					m_year = Integer.parseInt(s[0]);
					m_month = Integer.parseInt(s[1]);
					m_day = Integer.parseInt(s[2]);
					w.cancel();
				}
			});
		}
	};

	OnClickListener timeDialog = new OnClickListener() {
		@Override
		public void onClick(View v) {
			final WheelTime w = new WheelTime(SmsActivity.this);
			w.show(time.getText().toString(), new OnClickListener() {
				@Override
				public void onClick(View v) {
					time.setText(w.getResult());
					String t = w.getResult();
					String[] s = t.split(":");
					m_hour = Integer.parseInt(s[0]);
					m_minute = Integer.parseInt(s[1]);
					w.cancel();
				}
			});
		}
	};

	OnClickListener cancelClick = new OnClickListener() {
		@Override
		public void onClick(View v) {
			finish();
		}
	};

	public void getTime(String time) {
		SimpleDateFormat yy = new SimpleDateFormat("yyyy");
		SimpleDateFormat mm = new SimpleDateFormat("MM");
		SimpleDateFormat dd = new SimpleDateFormat("dd");
		SimpleDateFormat hh = new SimpleDateFormat("HH");
		SimpleDateFormat mi = new SimpleDateFormat("mm");
		Date d = new Date(Long.parseLong(time));
		m_year = Integer.parseInt(yy.format(d));
		m_month = Integer.parseInt(mm.format(d));
		m_day = Integer.parseInt(dd.format(d));
		m_hour = Integer.parseInt(hh.format(d));
		m_minute = Integer.parseInt(mi.format(d));
	}

	private void titleOPeration() {
		Button save = (Button) findViewById(R.id.save);
		save.setOnClickListener(saveListener);
		if (operateType == 1 || operateType == 2) {
			save.setText("保存");
			if (type == 1) {
				setTitle("新增" + name + "收件");
			} else if (type == 2) {
				setTitle("新增" + name + "发件");
			}
		} else if (operateType == 3 || operateType == 4) {
			save.setText("修改");
			if (type == 1) {
				setTitle("修改" + name + "收件");
			} else if (type == 2) {
				setTitle("修改" + name + "发件");
			}
		} else if (operateType == 5) {
			save.setText("发送");
			setTitle("给" + name + "发送消息");
		}
	}

}
